package com.yiku.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:ludada
 * @Date:2022/08/15 15:06
 */
@EnableWebMvc
//@EnableSwagger2WebMvc
@Configuration
public class SwaggerConfig {
    //根据实际使用的是生产环境还是运营环境
    // 设置要显示的Swagger环境
    /**
     * 过滤接口
     */
    private static final List<String> excludedPathPrefix = Arrays.asList(
            "/index"
    );

    @Bean
    public Docket getDocket(Environment environment) {
        Profiles profiles = Profiles.of("dev");
        // 通过environment.acceptsProfiles();判断自己是否在自己设定换的环境当中
        boolean isOpen = environment.acceptsProfiles(profiles);
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
//        aParameterBuilder.name("Authorization").description("Authorization").modelRef(new ModelRef("string")).parameterType("header").build();
//        List<Parameter> aParameters = Lists.newArrayList();
//        aParameters.add(aParameterBuilder.build());

        DocumentationType swagger2 = DocumentationType.SWAGGER_2;
        Docket docket = new Docket(swagger2).select()
                .apis(RequestHandlerSelectors.basePackage("com.yiku.controller"))
                .paths(PathSelectors.any())
//                .paths((s) -> {
//                    for(String pathPrefix : excludedPathPrefix) {
//                        if(StringUtils.startsWith(s, pathPrefix)) {
//                            return false;
//                        }
//                    }
//                    return true;
//                })

                .build().apiInfo(apiInfo()).groupName("义库").enable(isOpen);
//                .globalOperationParameters(aParameters);
        return docket;
    }


    /**
     * 配置Swagger信息
     *4
     * @return
     */
    private ApiInfo apiInfo() {
        // 配置作者信息
        Contact contact = new Contact("义库",
                "网址",
                "邮箱地址");
        // 配置API文档标题
        return new ApiInfo("双童钉钉审批互通后台", "描述", "版本", "url", contact, "许可证", "url", new ArrayList<>());


    }
//    /**
//     * 设置授权信息
//     */
//    private List<SecurityScheme> securitySchemes() {
//        ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", io.swagger.v3.oas.models.security.SecurityScheme.In.HEADER.toString());
//        return Collections.singletonList(apiKey);
//    }
//
//    /**
//     * 授权信息全局应用
//     */
//    private List<SecurityContext> securityContexts() {
//        return Collections.singletonList(
//                SecurityContext.builder()
//                        .securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")})))
//                        .build()
//        );
//    }
//
//    @SafeVarargs
//    private final <T> Set<T> newHashSet(T... ts) {
//        if (ts.length > 0) {
//            return new LinkedHashSet<>(Arrays.asList(ts));
//        }
//        return null;
//    }
//
//    /**
//     * 通用拦截器排除swagger设置，所有拦截器都会自动加swagger相关的资源排除信息
//     */
//    @SuppressWarnings("unchecked")
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        try {
//            Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations",true);
//            List<InterceptorRegistration> registrations = (List<InterceptorRegistration>) ReflectionUtils.getField(registrationsField, registry);
//            if (registrations != null) {
//                for (InterceptorRegistration interceptorRegistration : registrations) {
//                    interceptorRegistration
//                            .excludePathPatterns("/swagger**/**")
//                            .excludePathPatterns("/webjars/**")
//                            .excludePathPatterns("/v3/**")
//                            .excludePathPatterns("/doc.html");
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//

}
